<template>
  <div class="echarts-demo">
    <h1>echarts示例</h1>
    <div ref="myCharts" style="width: 800px; height: 600px; border: 1px solid;"></div>
    <div ref="pieCharts" style="width: 800px; height: 600px; border: 1px solid;"></div>

  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import * as echarts from 'echarts'

// 定义响应式数据 ref 对象
const myCharts = ref()
const pieCharts = ref()

/**
 * 挂载后执行
 */
onMounted(() => {
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(myCharts.value);
    // 绘制图表
    myChart.setOption({
        title: {
            text: '产品销量图'
        },
        tooltip: {},
        legend: {},
        xAxis: {
            data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
            {
                name: '销量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            },
            {
                name: '销量2',
                type: 'line',
                data: [10, 15, 30, 15, 20, 10]
            }
        ]
    });
})

onMounted(() => {
    var myChart = echarts.init(pieCharts.value);
    var option;

    option = {
        tooltip: {
            trigger: 'item'
        },
        legend: {
            top: '5%',
            left: 'center'
        },
        series: [
            {
            name: '销量',
            type: 'pie',
            radius: ['30%', '70%'],
            avoidLabelOverlap: false,
            padAngle: 5,
            itemStyle: {
                borderRadius: 10
            },
            label: {
                show: false,
                position: 'center'
            },
            emphasis: {
                label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold'
                }
            },
            labelLine: {
                show: false
            },
            data: [
                { value: 5, name: '衬衫' },
                { value: 20, name: '羊毛衫' },
                { value: 36, name: '雪纺衫' },
                { value: 20, name: '裤子' },
                { value: 10, name: '高跟鞋' },
                { value: 30, name: '袜子' },
            ]
            }
        ]
    };

    option && myChart.setOption(option);
})
</script>